﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq.Expressions;

namespace Eswat.Client.Wpf.ViewModels
{
	public abstract class ViewModelBase : INotifyPropertyChanged
	{
		/// <summary>
		/// Occurs when a property value changes.
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		/// Called when [property changed].
		/// </summary>
		/// <typeparam name="TProperty">The type of the property.</typeparam>
		/// <param name="propertyExpression">The property expression.</param>
		protected virtual void OnPropertyChanged<TProperty>(Expression<Func<TProperty>> propertyExpression)
		{
			var memberExpression = propertyExpression.Body as MemberExpression;

			if (memberExpression == null) throw new InvalidCastException("propertyExpression is not a valid instance of MemberExpression");

			if (this.PropertyChanged != null)
			{
				var propertyName = memberExpression.Member.Name;
				var eventArgs = new PropertyChangedEventArgs(propertyName);

				this.PropertyChanged(this, eventArgs);
			}
		}

		/// <summary>
		/// Formats the specified value with the specified args.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="args">The args.</param>
		/// <returns></returns>
		protected static string FormatWith(string value, params object[] args)
		{
			var newValue = string.Format(CultureInfo.InvariantCulture, value, args);

			return newValue;
		}
	}
}
